package the.workshop.bu.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import the.broccoli.setting.service.IFileService;
import the.workshop.bu.constant.BuConsts;
import the.workshop.bu.dto.CustomerDTO;
import the.workshop.bu.dto.CustomerInfoDTO;
import the.workshop.bu.dto.CustomerTaxCondition;
import the.workshop.bu.entity.Customer;
import the.workshop.bu.entity.CustomerLegal;
import the.workshop.bu.service.ICustomerInfoService;
import the.workshop.bu.service.ICustomerLegalService;
import the.workshop.bu.service.ICustomerService;
import the.workshop.bu.service.ICustomerTaxService;

import javax.annotation.Resource;

@Service
public class CustomerInfoServiceImpl implements ICustomerInfoService {

    @Resource
    private ICustomerService customerService;

    @Resource
    private ICustomerLegalService customerLegalService;

    @Resource
    private ICustomerTaxService customerTaxService;

    @Resource
    private IFileService fileService;

    @Override
    public CustomerInfoDTO detailById(Integer customerId) {
        CustomerInfoDTO customerInfoDTO = new CustomerInfoDTO();

        Customer customer = customerService.getById(customerId);
        customerInfoDTO.setCustomer(customer);

        /*CustomerDTO customerDTO = customerService.detailById(customerId);
        if (customerDTO != null) {
            Customer customer = new Customer();
            BeanUtils.copyProperties(customerDTO, customer);
            customerInfoDTO.setCustomer(customer);
        }*/

        CustomerLegal customerLegal = customerLegalService.getById(customerId);
        if (customerLegal != null) {
            customerInfoDTO.setCustomerLegal(customerLegal);
        }

        CustomerTaxCondition customerTaxCondition = new CustomerTaxCondition();
        customerTaxCondition.setCustomerId(customerId);
        customerInfoDTO.setTaxes(customerTaxService.listByCondition(customerTaxCondition));

        customerInfoDTO.setFiles(fileService.selectByTargetId(BuConsts.CUSTOMER_FILE_CATEGORY, customerId.toString()));

        return customerInfoDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(CustomerInfoDTO dto) {
        Customer customer = dto.getCustomer();

        // 公司信息
        customerService.updateById(customer);

        // 法人信息
        if (customerLegalService.getById(dto.getCustomer().getId()) == null) {
            customerLegalService.save(dto.getCustomerLegal());
        } else {
            customerLegalService.updateById(dto.getCustomerLegal());
        }

        // 税
        customerTaxService.deleteByCustomerId(customer.getId(), customer.getModifiedId(), customer.getModifiedName());
        if (!CollectionUtils.isEmpty(dto.getTaxes())) {
            customerTaxService.saveBatch(dto.getTaxes());
        }

        // 客户附件
        fileService.deleteByTargetId(BuConsts.CUSTOMER_FILE_CATEGORY, customer.getId().toString(), customer.getModifiedId(), customer.getModifiedName());
        if (!CollectionUtils.isEmpty(dto.getFiles())) {
            fileService.saveBatch(dto.getFiles());
        }
    }
}
